package com.itboye.eweilisten.me.timu.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.util.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 语句判断
 */
public class YuJvPanDuanAdapter extends RecyclerView.Adapter<YuJvPanDuanAdapter.MyViewHolder> {
    private Context context;
    private List<TIbean> beans;

    public YuJvPanDuanAdapter(Context context, List<TIbean> beans) {
        this.context = context;
        this.beans = beans;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_danxuan_yujv, parent, false);
        YuJvPanDuanAdapter.MyViewHolder myViewHolder = new YuJvPanDuanAdapter.MyViewHolder(view);
        return myViewHolder;
    }
    @Override
    public void onBindViewHolder(YuJvPanDuanAdapter.MyViewHolder holder, int position) {
        holder.timu_num.setText(position+1+"");
        if (!beans.get(position).getQuestion().equals("")){
            holder.question.setVisibility(View.VISIBLE);
            holder.question.setText(beans.get(position).getQuestion());
            holder.question.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_01));
        }else{
            holder.question.setVisibility(View.GONE);
        }
        holder.answerLine.removeAllViews();
        holder.questionLine.removeAllViews();
        holder.answerLine.removeAllViews();
        for (int j = 0; j < beans.get(position).getContent().size(); j++) {
            questionContentAdd(holder.questionLine, beans.get(position).getContent().get(j));
        }
        for (int j = 0; j < beans.get(position).getOption().size(); j++) {
            answerLineContentAdd(holder.answerLine, beans.get(position).getOption(), beans.get(position).getOption().get(j).getOptions(), j, position);
        }
    }
    /**
     * 内容
     * @param questionLine
     * @param string
     */
    private void questionContentAdd(final FlowLayout questionLine, String string) {
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 0, 5);

        if ("_".equals(string)) {
            TextView textView = new TextView(context);
            textView.setText(string + "__");
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#3b7830"));
            textView.setPadding(15, 0, 15, 0);
            textView.setLayoutParams(params);
            questionLine.addView(textView);

        } else {
            TextView textView = new TextView(context);
            textView.setText(string);
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#3b7830"));
            textView.setLayoutParams(params);
            questionLine.addView(textView);
        }
    }
    /**
     * 答案
     * @param answerLine
     * @param string
     */
    private void answerLineContentAdd(LinearLayout answerLine, final ArrayList<TIbean.Options> qianzhui, String string, final int j, final int position) {
        // 设置添加的textvIew的参数
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT,1.0f);
        params.setMargins(70, 5, 70, 5);
        final TextView textView = new TextView(context);
        textView.setText(string);
        textView.setTextSize(20);
        textView.setTextColor(Color.parseColor("#ffffff"));
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(params);
        textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_04));
        answerLine.addView(textView);

        if (qianzhui.get(j).isDa()) {
            textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_01));
        } else {
            textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_04));
        }
        textView.setTag(R.id.tag, qianzhui.get(j).getOptions());
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < qianzhui.size(); i++) {
                    qianzhui.get(i).setDa(false);
                }
                qianzhui.get(j).setDa(true);
                ArrayList<String> answer_truev = beans.get(position).getAnwser_true();
                int poistions = answer_truev.indexOf(textView.getTag(R.id.tag).toString());
                if (poistions == -1) {
                    beans.get(position).setXuan(1);//1代表选择错误
                } else {
                    beans.get(position).setXuan(0);//0代表选择正确
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    // 自定义的ViewHolder，持有每个Item的的所有界面元素
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public FlowLayout questionLine;
        public LinearLayout answerLine;
        public TextView timu_num;
        public TextView question;

        public MyViewHolder(View itemView) {
            super(itemView);
            questionLine = (FlowLayout) itemView.findViewById(R.id.questionLine);
            answerLine = (LinearLayout) itemView.findViewById(R.id.answerLine);
            timu_num = (TextView) itemView.findViewById(R.id.timu_num);
            question = (TextView) itemView.findViewById(R.id.question);
        }
    }
}
